package com.work.game.home.ui.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.work.game.R;
import com.work.game.config.UrlConfig;
import com.work.game.home.ui.enums.RedisEnum;
import com.work.game.home.ui.enums.RiskEnum;
import com.work.game.utils.QrcodeUtils;
import com.work.game.volley.BaseActivity;
import com.work.game.volley.state.HUCState;

import cn.hutool.core.util.StrUtil;

public class RewardActivity extends BaseActivity {
    TextView tvHongbao;
    TextView im;

    private AlertDialog alertDialog;

    private TextView qqQun;
    private String key;
    private ImageView ivQrCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_reward);
        super.onCreate(savedInstanceState);
        tvActionbarTitle.setText("奖励中心");
        tvHongbao.setText(getSharedPreferences("moneyRedPacket"));
        im.setText(getSharedPreferences("moneyGoldCoin"));
        qqQun = findViewById(R.id.tv_qq_qun_content);
        View llService = findViewById(R.id.ll_service);
        String group = getSharedPreferences(RedisEnum.APP_QQ_COMMUNICATION_GROUP.getKey());
        if (StrUtil.isEmpty(group)) {
            findViewById(R.id.ll_qq_qun).setVisibility(View.GONE);
        }
        String service = getSharedPreferences(RedisEnum.APP_QQ_CUSTOMER_SERVICE.getKey());
        if (StrUtil.isEmpty(service)) {
            llService.setVisibility(View.GONE);
        }
        Bitmap qrCode = QrcodeUtils.createQRImage(service, 500, 500);
        ivQrCode.setImageBitmap(qrCode);
        long lastPopupTime = getSharedPreferencesLong("showService");
        // 如果从未显示过弹窗，或者已经过了24小时
        if (lastPopupTime == 0 || System.currentTimeMillis() - lastPopupTime > 24 * 60 * 60 * 1000 && StrUtil.isNotEmpty(service)) {
            // 显示弹窗
            show();
        }
    }

    @Override
    public void initClick() {
        super.initClick();
        findViewById(R.id.tv_click_jb).setOnClickListener(this);
        findViewById(R.id.tv_click_hb).setOnClickListener(this);
        findViewById(R.id.tv_qq_qun).setOnClickListener(this);
        findViewById(R.id.tv_qq_hao).setOnClickListener(this);
    }

    @Override
    public void initView() {
        super.initView();
        im = findViewById(R.id.income_mgc);
        tvHongbao = findViewById(R.id.tv_hongbao);
        ivQrCode = findViewById(R.id.iv_qr_code);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_click_jb:
                sendIntent(this, WithdrawalActivity.class, String.valueOf(HUCState.HUC_REQEUST_CODE_1));
                break;
            case R.id.tv_click_hb:
                sendIntent(this, WithdrawalActivity.class, String.valueOf(HUCState.HUC_REQEUST_CODE_2));
                break;
            case R.id.tv_qq_qun:
                joinQQGroup(key);
                break;
        }
    }


    /****************
     *
     * 发起添加群流程。群号：嘉凡游戏(711365196) 的 key 为： i7mUDfR-RO5p8BVryPego2_YzAB4Tg2T
     * 调用 joinQQGroup(i7mUDfR-RO5p8BVryPego2_YzAB4Tg2T) 即可发起手Q客户端申请加群 嘉凡游戏(711365196)
     *
     * @param key 由官网生成的key
     * @return 返回true表示呼起手Q成功，返回false表示呼起失败
     ******************/
    public void joinQQGroup(String key) {
        Intent intent = new Intent();
        intent.setData(Uri.parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26jump_from%3Dwebapi%26k%3D" + key));
        // 此Flag可根据具体产品需要自定义，如设置，则在加群界面按返回，返回手Q主界面，不设置，按返回会返回到呼起产品界面    //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        try {
            startActivity(intent);
        } catch (Exception e) {
            // 未安装手Q或安装的版本不支持
            showToast("您还没有安装QQ，请先安装软件");
        }
    }


    private void show() {
        //判断提现账号
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        // dialog.setTitle("绑定姓名");
        View root = LayoutInflater.from(this).inflate(R.layout.pop_qq_qun, null);
        dialog.setView(root);
        root.findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                setSharedPreferences("showService", System.currentTimeMillis());
            }
        });
        alertDialog = dialog.create();
        alertDialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
        alertDialog.setCancelable(false);
        alertDialog.show();
    }
}